home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1990 / number5 / textify.bas < prev    next >
BASIC Source File  |  1990-09-14  |  6KB  |  167 lines

  1. '     TEXTIFY.BAS
  2. '     by Robert Stearns
  3. '     For Turbo Basic/PowerBASIC
  4. '
  5. '     This program will take any file and create a BASIC program
  6. '     which will recreate the file, but which contains no characters
  7. '     that could cause any communications link a problem. The only
  8. '     characters in the file are those from the 95 character graphic
  9. '     subset of the ASCII set, and many of the more obscure
  10. '     characters from that group have been eliminated as well. Any
  11. '     file created with program should pass through almost any
  12. '     communications link unscathed. I even made sure the maximum
  13. '     line length was less than 72.
  14. '
  15. defint a-z                   ' all integers makes everything faster
  16. dim table$(63)               ' the character conversion table
  17. chunksize=36                 ' handle the file in pieces this size
  18. a$=command$                  ' get the name of the file to convert
  19. '
  20. '     if the file name is not present in the command line, get it
  21. '     from the user interactively.
  22. '
  23. if a$="" then
  24.      input "Type the name of the file to process";a$
  25. end if
  26. open a$ for binary as 1
  27. '
  28. '     the output file name will be the same as the input file name,
  29. '     including path, but with the extension BAS.
  30. '
  31. i=instr(a$,".")
  32. if i=0 then b$=a$ else b$=left$(a$,i-1)
  33. b$=b$+".BAS"
  34. open b$ for output as 2
  35. '
  36. '     move the selected characters to the array to simplify and
  37. '     even speed up their access.
  38. '
  39. read tb$,tbx$
  40. tb$=tb$+tbx$
  41. for i=0 to 63
  42.      table$(i)=mid$(tb$,i+1,1)
  43. next i
  44. '
  45. '     read the conversion program from the data statements and
  46. '     write it as the prefix to the converted data.
  47. '
  48. do
  49.      read bline$
  50.      print #2,bline$
  51. loop until bline$=" 9999 '"
  52. '
  53. '    write the first data statement containing the file length and
  54. '    file name to the converted file.
  55. '
  56. filelen!=lof(1)
  57. print #2,using "##### DATA ";10000;
  58. print #2,a$,",",filelen!
  59. print filelen!;" bytes to do"' Tell the user how much there is to do
  60. lineno=10001                 ' The line number in the output program
  61. filepos!=1                   ' Current position in the input file
  62. lim!=1000                    ' When to tell how much we've done
  63. '
  64. '     This is the main code of the program. It reads chunks of the
  65. '     input file, converts each group of three bytes to four
  66. '     characters in the output file, and writes the characters to
  67. '     the output file in the form of data statements. As the input
  68. '     is processed, a checksum is formed for each chunk and the
  69. '     checksum is written to the output file to be checked by the
  70. '     program which will reconstruct the file.
  71. '
  72. while(filepos!<=filelen!)
  73.      seek #1,filepos!-1
  74.      if filepos!+chunksize-1<=filelen! then
  75.           get$ #1,chunksize,t$
  76.      else
  77.           get$ #1,filelen!-filepos!+1,t$
  78.       i=len(t$) mod 3          ' for the rest of the code to
  79.       if i=0 then i=3          ' work properly, there must be a
  80.       t$=t$+left$("   ",3-i)   ' multiple of 3 chars in t$
  81.      end if
  82.      print #2,using "##### DATA ";lineno;
  83.      lineno=lineno+1
  84.      checksum=0
  85.      for i=1 to len(t$) step 3
  86.           j1=asc(mid$(t$,i  ,1))          'aaaaaaaabbbbbbbbcccccccc
  87.           j2=asc(mid$(t$,i+1,1))          '111111222222333333444444
  88.           j3=asc(mid$(t$,i+2,1))          ' as the above bit map
  89.           c1=                    j1 \ 4   ' shows, we will convert
  90.           c2=((j1 and 3)  * 16 )+j2 \ 16  ' 24 bits of three data
  91.       c3=((j2 and 15) * 4  )+j3 \ 64  ' bytes to four numbers
  92.           c4= (j3 and 63)                 ' between 0 and 63.
  93.           print #2,table$(c1);            ' Next, we print them as
  94.           print #2,table$(c2);          ' characters which can be
  95.           print #2,table$(c3);          ' converted back to the
  96.           print #2,table$(c4);          ' corresponding numbers.
  97.           checksum = checksum+j1+j2+j3    ' always <= chunksize*255
  98.      next i
  99.      print #2,",";checksum
  100.      filepos!=filepos!+chunksize
  101.      if filepos!>lim! then
  102.           print filepos!;" bytes done"
  103.           lim!=lim!+1000
  104.      end if
  105. wend
  106. '
  107. '     put on the final data statement indicating the end of the
  108. '     file, close the files and tell the user we are done
  109. '
  110. print #2,using "##### DATA ";lineno;
  111. print #2,chr$(34);chr$(34);",0"
  112. close #1
  113. close #2
  114. print "Files closed, job complete"
  115. stop
  116. '
  117. '     The characters to which the file is converted
  118. '
  119. data ABCDEFGHIJKLMNOPQRSTUVWXYZ
  120. data abcdefghijklmnopqrstuvwxyz0123456789@$
  121. '
  122. '    The file reconstruction program less the data statements
  123. '    which describe the file to be built. This program does the
  124. '    inverse transform of the program above. It processes each
  125. '    group of four characters into 6 bit integers, then concatenates
  126. '    consecutive groups of 8 bits into output characters. These
  127. '    output characters are then written to the output file until
  128. '    the original file size is reached.
  129. '
  130. data "   10 DEFINT A-Z"
  131. data "   15 READ TB$,TBX$,FC$,JS$,JE$"
  132. data "   16 TB$=TB$+TBX$"
  133. data "   20 READ A$"
  134. data "   30 OPEN A$ FOR OUTPUT AS #1"
  135. data "   40 READ FS!"
  136. data "   45 PRINT JS$;CHR$(32);A$"
  137. data "   50 READ LN$,CS"
  138. data "   55 L=10001"
  139. data "   60 WHILE(LEN(LN$)<>0)"
  140. data "   65      CC=0"
  141. data "   70      FOR I=1 TO LEN(LN$) STEP 4"
  142. data "   80           D1=INSTR(TB$,MID$(LN$,I  ,1))-1"
  143. data "   90           D2=INSTR(TB$,MID$(LN$,I+1,1))-1"
  144. data "  100           D3=INSTR(TB$,MID$(LN$,I+2,1))-1"
  145. data "  110           D4=INSTR(TB$,MID$(LN$,I+3,1))-1"
  146. data "  120           C1=((D1* 4) + (D2 \ 16)) AND 255"
  147. data "  130           C2=((D2*16) + (D3 \  4)) AND 255"
  148. data "  140           C3=((D3*64) +  D4      ) AND 255"
  149. data "  145           CC=CC+C1+C2+C3"
  150. data "  150           PRINT #1,CHR$(C1);"
  151. data "  160           X!=X!+1"
  152. data "  170           IF X!<FS! THEN PRINT #1,CHR$(C2); : X!=X!+1"
  153. data "  180           IF X!<FS! THEN PRINT #1,CHR$(C3); : X!=X!+1"
  154. data "  190      NEXT I"
  155. data "  195      IF CC<>CS THEN PRINT FC$;L"
  156. data "  200      READ LN$,CS"
  157. data "  205      L=L+1"
  158. data "  210 WEND"
  159. data "  220 CLOSE #1"
  160. data "  225 PRINT A$;CHR$(32);JE$"
  161. data "  230 STOP"
  162. data " 1000 DATA ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  163. data " 1005 DATA abcdefghijklmnopqrstuvwxyz0123456789@$"
  164. data " 1010 DATA FILE CORRUPTED AT"
  165. data " 1020 DATA CREATING FILE"
  166. data " 1030 DATA HAS BEEN CREATED"
  167. data " 9999 '"